package com.appjishu.web.biz.service;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.appjishu.web.biz.cache.BussinessCache;
import com.appjishu.web.biz.dao.UserMapper;
import com.appjishu.web.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserService{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BussinessCache cache;
    public void changePassword(String password,Long userId) {
        userMapper.changePassword(password,userId);
    }

    public void importExcelData(InputStream excelFilePath) {
        // 使用Hutool读取Excel数据
        ExcelReader reader = ExcelUtil.getReader(excelFilePath);
        List<List<Object>> excelData = reader.read(1);
        List<User> list = new ArrayList<>();
        // 遍历Excel数据并插入数据库
        for (List<Object> rowData : excelData) {
            // 转换为实体对象
            User entity = new User();
            entity.setUsername(String.valueOf(rowData.get(0)));
            entity.setPassword(String.valueOf(String.valueOf(rowData.get(7))==null&&"".equals(String.valueOf(rowData.get(7)))?"e10adc3949ba59abbe56e057f20f883e":String.valueOf(rowData.get(7))));
            entity.setEmail(String.valueOf(rowData.get(1)));
            entity.setAvatar(String.valueOf(rowData.get(2)));
            entity.setName((String.valueOf(rowData.get(3))));
            entity.setClassName(String.valueOf(rowData.get(6)));

            entity.setClassId(cache.getClassByName(String.valueOf(rowData.get(6))));
            entity.setSpecialityId(cache.getSpecialityByName(String.valueOf(rowData.get(5))));
            entity.setCollegeId(cache.getCollegeByName(String.valueOf(rowData.get(4))));
            list.add(entity);
        }
        userMapper.batchInsetUser(list);
        reader.close();
    }

    public void importExcelDataNew(InputStream excelFilePath) {
        // 使用Hutool读取Excel数据
        ExcelReader reader = ExcelUtil.getReader(excelFilePath);
        List<List<Object>> excelData = reader.read(1);
        List<User> list = new ArrayList<>();
        // 遍历Excel数据并插入数据库
        for (List<Object> rowData : excelData) {
            // 转换为实体对象
            User entity = new User();
//            entity.setUsername(String.valueOf(rowData.get(1)));
//            entity.setEmail(String.valueOf(rowData.get(2)));
//            entity.setPassword(String.valueOf(String.valueOf(rowData.get(3)) == null && "".equals(String.valueOf
//            (rowData.get(3))) ? "e10adc3949ba59abbe56e057f20f883e" : String.valueOf(rowData.get(3))));
//            entity.setName((String.valueOf(rowData.get(4))));
//            entity.setScore(Integer.valueOf(String.valueOf(rowData.get(5))));
//            entity.setClassId(cache.getClassByName(String.valueOf(rowData.get(6))));
//            entity.setSpecialityId(cache.getSpecialityByName(String.valueOf(rowData.get(7))));
//            entity.setCollegeId(cache.getCollegeByName(String.valueOf(rowData.get(8))));
//            entity.setClassName(String.valueOf(rowData.get(9)));
//            entity.setPersonalSignature(String.valueOf(rowData.get(10)));
//            entity.setAvatar(String.valueOf(rowData.get(11)));
//            entity.setStuNumber(String.valueOf(rowData.get(12)));

            entity.setPassword("e10adc3949ba59abbe56e057f20f883e");
            entity.setName((String.valueOf(rowData.get(2))));
            entity.setScore(0);
            entity.setClassId(cache.getClassByName(String.valueOf(rowData.get(8))));
            entity.setSpecialityId(cache.getSpecialityByName(String.valueOf(rowData.get(6))));
            entity.setCollegeId(cache.getCollegeByName(String.valueOf(rowData.get(7))));
            entity.setClassName(String.valueOf(rowData.get(8)));
            entity.setPersonalSignature("知识改变命运");
            entity.setAvatar("https://up.enterdesk.com/edpic_source/e3/22/a0/e322a00af2e90d7023df12b52b512fdb.jpg");
            entity.setStuNumber(String.valueOf(rowData.get(1)));
            entity.setCreateTime(LocalDateTime.now());
            entity.setUpdateTime(LocalDateTime.now());
            list.add(entity);
        }
        userMapper.batchInsetUserNew(list);
        reader.close();
    }

    public void setLoginInformation(Long userId, Timestamp loginTime, Long onlineTimes, String deviceType) {
        userMapper.setLoginInformation(userId, loginTime, onlineTimes, deviceType);
    }

    public void setDownLineInformation(Long userId, Timestamp downlineTime, Long duration, Long allDuration) {
        userMapper.setDownLineInformation(userId, downlineTime, duration, allDuration);
    }

    public void setLoginDownInformation(Long userId, Timestamp loginTime, Long onlineTimes, String deviceType,
                                        Timestamp downlineTime, Long duration, Long allDuration) {
        userMapper.setLoginDownInformation(userId, loginTime, onlineTimes, deviceType, downlineTime, duration,
                allDuration);
    }

    public User selectAll(Long userId) {
        return userMapper.selectAll(userId);
    }

    public void setUserInformation(Long userId, Timestamp loginTime, Timestamp downlineTime, String deviceType,
                                   Long duration) {
        userMapper.setUserInformation(userId, loginTime, downlineTime, deviceType, duration);
    }
}
